﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;

namespace PeiYue.Api.Controllers.Tms
{
    /// <summary>
    /// 司机管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class DriverController : BaseController<DriverDao>
    {
        readonly IDriverServices _driverServices;
        readonly IVehicleServices _vehicleServices;
        readonly IUser _user;
        readonly IMapper _mapper;
        public DriverController(IDriverServices driverServices, IVehicleServices vehicleServices, IUser user, IMapper mapper):base(driverServices,user)
        {
            _driverServices = driverServices;
            _vehicleServices = vehicleServices;
            _user = user;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取司机详情
        /// </summary>
        /// <param name="id">司机id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<SearchDriverResponseDto>> Get(long id)
        {
            var model = await _driverServices.QueryById(id);
            var dto = _mapper.Map<SearchDriverResponseDto>(model);
            return new MessageModel<SearchDriverResponseDto>()
            {
                msg = "获取成功",
                success = true,
                response = dto
            };
        }
        /// <summary>
        /// 获取司机分页列表
        /// </summary>
        /// <param name="requestDto">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<SearchDriverResponseDto>>> GetList([FromQuery]SearchDriverRequestDto requestDto)
        {
            var data = await _driverServices.GetPage(requestDto);
            return new MessageModel<PageModel<SearchDriverResponseDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }
        /// <summary>
        /// 新增司机
        /// </summary>
        /// <param name="driverDto">新增司机参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] UpdateDriverDto driverDto)
        {
            var data = new MessageModel<string>();
            var list = await _driverServices.Query(t =>t.vc_id == driverDto.vc_id && t.status == (int)CommonEnum.Status.Enable);
            if (list.Count > 0)
            {
                var vehicleDao = await _vehicleServices.QueryById(driverDto.vc_id);
                data.msg = $"[{vehicleDao?.names}]车辆已经关联了司机，一辆车只能关联一个司机";
                data.status = 500;
                data.success = false;
                return data;
            }
            var driverDao = _mapper.Map<DriverDao>(driverDto);
            driverDao.create_user = _user.ID;
            driverDao.update_user = _user.ID;
            driverDao.id = await _driverServices.GenId();
            await _driverServices.Add(driverDao);
            if (driverDao.id > 0)
            {
                data.success = true;
                data.msg = "添加成功";
            }
            return data;
        }
        /// <summary>
        /// 修改司机
        /// </summary>
        /// <param name="driverDto">修改司机参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Update([FromBody] UpdateDriverDto driverDto)
        {
            var data = new MessageModel<string>();
            var driverDao = _mapper.Map<DriverDao>(driverDto);
            if (driverDao != null && driverDao.id > 0)
            {
                var list = await _driverServices.Query(t => t.id != driverDao.id && t.vc_id == driverDao.vc_id && t.status == (int)CommonEnum.Status.Enable);
                if (list.Count > 0)
                {
                    var vehicleDao = await _vehicleServices.QueryById(driverDto.vc_id);
                    data.msg = $"[{vehicleDao?.names}]车辆已经关联了司机，一辆车只能关联一个司机";
                    data.status = 500;
                    data.success = false;
                    return data;
                }
                driverDao.update_user = _user.ID;
                data.success = await _driverServices.Update(driverDao, null, new List<string> { "status", "create_time", "create_user" });
                if (data.success)
                {
                    data.msg = "更新成功";
                    data.response = driverDao?.id.ObjToString();
                }
            }
            return data;
        }
        /// <summary>
        /// 获取司机下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<List<SelectItemDto>>> GetSelectItem()
        {
            var data = await _driverServices.Query(p => p.status == (int)CommonEnum.Status.Enable);
            var result = data.Select(p => new SelectItemDto { Id = p.id, Name = p.names }).ToList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = result
            };
        }
    }
}